package cn.edu.zucc.online_market.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import cn.edu.zucc.online_market.online_marketUtil;
import cn.edu.zucc.online_market.model.BeanProduct;
import cn.edu.zucc.online_market.model.BeanProductClass;
import cn.edu.zucc.online_market.model.BeanUser;
import cn.edu.zucc.online_market.util.BaseException;

public class FrmMain extends JFrame implements ActionListener {

	private JMenuBar menubar = new JMenuBar();
	private JMenu menu_product_browse = new JMenu("product browse");
	private JMenu menu_address_manager = new JMenu("address manager");
	private JMenu menu_order_manager = new JMenu("order manager");
	private JMenu menu_more = new JMenu("more");
//  product 
	private JMenuItem menuItem_product_classfication = new JMenuItem("product classfication");
	private JMenuItem menuItem_product_query = new JMenuItem("product query");
//	address_manager
	private JMenuItem menuItem_user_add_shipping_address = new JMenuItem("add shipping address");
	private JMenuItem menuItem_user_change_shipping_address = new JMenuItem("change shipping address");
//	order_manager	
	private JMenuItem menuItem_user_query_order = new JMenuItem("order query");
//	more
	private JMenuItem menuItem_user_change_pwd = new JMenuItem("change password");
	private JMenuItem menuItem_user_become_VIP = new JMenuItem("become VIP");
	
	private FrmLogin dlgLogin = null;
	private JPanel statusBar = new JPanel(); 
	
	private Object table_product_class_title[] = BeanProductClass.tableProductClassTitles;
	private Object table_product_class_data[][];
	
	DefaultTableModel table_product_class_model = new DefaultTableModel();
	private JTable data_Jtable_product_class = new JTable(table_product_class_model);
	
	private Object table_product_title[] = BeanProduct.tableProductTitles;
	private Object table_product_data[][];
	DefaultTableModel table_product_model = new DefaultTableModel();
	private JTable data_Jtable_product = new JTable(table_product_model);
	
	private BeanProductClass cur_product_class = null;
	List<BeanProductClass> all_product_class = null;
	List<BeanProduct> list_products = null;
	
	private void reload_product_class_table() {
		try {
			this.all_product_class = online_marketUtil.productClassManager.loadAll();
			
		}catch(BaseException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "����",JOptionPane.ERROR_MESSAGE);
			return;
		}	
		table_product_class_data = new Object[this.all_product_class.size()][BeanProductClass.tableProductClassTitles.length];
		int i,j;
		for( i=0;i<this.all_product_class.size();i++) {
			for(j=0;j<BeanProductClass.tableProductClassTitles.length;j++) {				
				this.table_product_class_data[i][j] = this.all_product_class.get(i).getCell(j);
			}			
		}
		this.table_product_class_model.setDataVector(this.table_product_class_data, this.table_product_class_title);
		this.data_Jtable_product_class.validate();
		this.data_Jtable_product_class.repaint();
	}
	private void reload_product_table(int product_class_index) {
		if(product_class_index<0) return;
		this.cur_product_class = this.all_product_class.get(product_class_index);
		try {
			System.out.print("product_class_index="+product_class_index);
			this.list_products = online_marketUtil.productManager.loadProductsByProductClass(cur_product_class);
		
		}catch(BaseException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "����",JOptionPane.ERROR_MESSAGE);
			return;
		}
		System.out.print("rowsize:"+this.list_products.size()+"colsize:"+BeanProduct.tableProductTitles.length);
		this.table_product_data = new Object[this.list_products.size()][BeanProduct.tableProductTitles.length];
		for(int i=0; i<this.list_products.size(); i++) {
			for(int j=0;j<BeanProduct.tableProductTitles.length;j++) {
				this.table_product_data[i][j]=this.list_products.get(i).getCell(j);
			}
			
		}
		this.table_product_model.setDataVector(this.table_product_data, this.table_product_title);
		this.data_Jtable_product.validate();
		this.data_Jtable_product.repaint();
	}
	
	public FrmMain( ) {
		// TODO Auto-generated method stub
		this.setExtendedState(Frame.MAXIMIZED_BOTH);
		this.setTitle("online market");
		this.dlgLogin = new FrmLogin(this,"login",true);
		this.dlgLogin.setVisible(true);
		
		this.menu_product_browse.add(this.menuItem_product_classfication); this.menuItem_product_classfication.addActionListener(this);
		this.menu_product_browse.add(this.menuItem_product_query); this.menuItem_product_query.addActionListener(this);
		
		this.menu_address_manager.add(this.menuItem_user_add_shipping_address); this.menuItem_user_add_shipping_address.addActionListener(this);
		this.menu_address_manager.add(this.menuItem_user_change_shipping_address); this.menuItem_user_change_shipping_address.addActionListener(this);
		
		this.menu_order_manager.add(this.menuItem_user_query_order); this.menuItem_user_query_order.addActionListener(this);
		
		this.menu_more.add(this.menuItem_user_become_VIP); this.menuItem_user_become_VIP.addActionListener(this);
		this.menu_more.add(this.menuItem_user_change_pwd); this.menuItem_user_change_pwd.addActionListener(this);
		
		this.menubar.add(this.menu_product_browse);
		this.menubar.add(this.menu_address_manager);
		this.menubar.add(this.menu_order_manager);
		this.menubar.add(this.menu_more);
		this.setJMenuBar(menubar);
		
		this.getContentPane().add(new JScrollPane(this.data_Jtable_product_class),BorderLayout.WEST);
		this.data_Jtable_product_class.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				int i = FrmMain.this.data_Jtable_product_class.getSelectedRow();
				if(i<0) {
					return;
				}
				FrmMain.this.reload_product_table(i);
			}
		});
		this.getContentPane().add(new JScrollPane(this.data_Jtable_product),BorderLayout.CENTER);
		this.reload_product_class_table();
		//status column
		this.statusBar.setLayout(new FlowLayout(FlowLayout.LEFT));
		JLabel label = new JLabel("Hello!"+BeanUser.currentLoginUser.getUser_name());
		this.statusBar.add(label);
		this.getContentPane().add(statusBar,BorderLayout.SOUTH);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		
		this.setVisible(true);
		JOptionPane.showMessageDialog(null, "Hello!", "This is FrmMain",JOptionPane.ERROR_MESSAGE);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}
